use crate::config::ActixConf;
use anyhow::anyhow;
use schemars::schema::RootSchema;
use serde::de::DeserializeOwned;
use serde_json::json;
use serde_yaml::Value;
use std::collections::HashMap;
use std::error::Error;
use std::io::Stderr;
// 用来处理yaml文件
// DeserializeOwned获取json字符串的所有权，避免引用
pub fn load_from_yaml<T>(rev_path: &str) -> anyhow::Result<T>
where
    T: DeserializeOwned,
{
    let schema = serde_yaml::from_str::<RootSchema>(&std::fs::read_to_string(rev_path)?)?;
    // println!("{:?}",json!(schema.schema.extensions));
    if let Ok(rs) = serde_json::from_str::<T>(serde_json::to_string(&schema.schema.extensions)?.as_str()) {
        return Ok(rs);
    }
    Err(anyhow!("no {} config found!", std::any::type_name::<T>()))
}

#[test]
fn test() {
    // let config = AppConfig::new("src/app.yml");
    // let result = config.get::<ActixConf>().unwrap();
    // println!("{:?}", result);
}
